﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebUI.DAL;
using System.Data.Linq;

namespace WebUI.BL
{
    public class ProductRelateBL
    {
        public void Insert(tbl_ProductRelate productRelate)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                try
                {
                    tbl_ProductRelate newProductRelate = new tbl_ProductRelate()
                    {
                        Status=productRelate.Status,
                        ProductRelateNo=productRelate.ProductRelateNo,
                        ProductRelateName=productRelate.ProductRelateName,
                        ProductNo=productRelate.ProductNo,
                        ProductName=productRelate.ProductName,
                        CreatedUser=productRelate.CreatedUser,
                        CreatedDate=productRelate.CreatedDate
                    };
                    contextdb.tbl_ProductRelates.InsertOnSubmit(newProductRelate);
                    contextdb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
        public void Update(tbl_ProductRelate productRelate)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var updateProductRelate = contextdb.tbl_ProductRelates.SingleOrDefault(u => u.RelateNo == productRelate.RelateNo);
                try
                {
                    if (updateProductRelate != null)
                    {
                        updateProductRelate.Status = productRelate.Status;
                        updateProductRelate.ProductRelateName = productRelate.ProductRelateName;
                        updateProductRelate.ProductNo = productRelate.ProductNo;
                        updateProductRelate.ProductName = productRelate.ProductName;
                        updateProductRelate.CreatedUser = productRelate.CreatedUser;
                        updateProductRelate.CreatedDate = productRelate.CreatedDate;

                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }

            }
        }
        public void Delete(int relateNo)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var deleteRelateNo = contextdb.tbl_ProductRelates.SingleOrDefault(u => u.RelateNo == relateNo);
                try
                {
                    if (deleteRelateNo != null)
                    {
                        contextdb.tbl_ProductRelates.DeleteOnSubmit(deleteRelateNo);
                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
    }
}
